/**
 * 
 */
package com.rs.controller;

import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;

import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.table.DefaultTableModel;

import com.github.pagehelper.PageInfo;
import com.rs.model.ZhAttendance;
import com.rs.service.ZhAttendanceService;
import com.rs.util.DateChooserJButton;
import com.rs.util.SpringContextUtils;

import javax.swing.SpringLayout;

import java.awt.event.ActionEvent;

import javax.swing.JTextField;

/**
 * @author zjw
 *
 */

@SuppressWarnings("serial")
public class AttendanceList extends JInternalFrame {
	
	

	private JPanel contentPane;
	
	private JScrollPane jp;
	
	private JTable jt;
	
	private Object[][] datas;
	
	ZhAttendance attendance = new ZhAttendance();
	int pageSum=1;
	
	ZhAttendanceService attendanceservice = SpringContextUtils.getBean(ZhAttendanceService.class);
	private JButton btn_start;
	private JButton btn_next;
	private JButton btn_end;
	private JLabel lb_page;
	String[] titles = {"主键","名称","早上","中午","下午","晚上","日期"};
	private JTextField txt_se_name;
	private DateChooserJButton txt_se_starttime;
	private DateChooserJButton txt_se_endtime;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AttendanceList frame = new AttendanceList();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AttendanceList() {
		setIconifiable(true);
		setMaximizable(true);
		setClosable(true);
		setResizable(false);
		
	
		setTitle("工作人员考勤列表");
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1054, 670);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);		
		
		jt = new JTable(new Object[][]{{null,null,null,null,null,null,null}},titles);
	
		
		 jt.setEnabled(true);
		jt.getColumnModel().getColumn(0).setPreferredWidth(0);
		jt.getColumnModel().getColumn(0).setMinWidth(0);
		jt.getColumnModel().getColumn(0).setMaxWidth(0);
		
		
		
		// Step1 添加行点击绑定事件 
		jt.addMouseListener(new MouseAdapter() { 
		@Override 
		public void mouseClicked(MouseEvent e) {                 
			rowClicked();             
			}
		/** *行点击事件 * 
		*<br>-----------------------------------------------------<br> *@author flotage 
		*@create 2013-4-17上午10:09:14 *@note */ 
		private void rowClicked() {
			// 获取被点击的行 
			int row = jt.getSelectedRow(); 
			// 获取点击行的内容 
			        DefaultTableModel dtm = (DefaultTableModel) jt.getModel();         
			        Object value =  dtm.getValueAt(row, 0);         
//			        AttendanceUpdate.showMessageDialog(null, value, "系统信息", AttendanceUpdate.INFORMATION_MESSAGE);  
			       
			        AttendanceUpdate att = new AttendanceUpdate();
			        att.select(value);
			        att.setVisible(true);
			
		}         
		});  
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
	
		
		
		jp = new JScrollPane(jt);
		sl_contentPane.putConstraint(SpringLayout.NORTH, jp, 150, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, jp, 5, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, jp, 550, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, jp, 1033, SpringLayout.WEST, contentPane);
		jp.setSize(854, 640);
		contentPane.add(jp);
		
		btn_start = new JButton("首页");
		btn_start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pageSum = 1;
				attendance.setPageNum(1);
				List<ZhAttendance> list = getAttendance(attendance).getList();
				SetTheValue(list);
			}
		});
		contentPane.add(btn_start);
		
		JButton btn_up = new JButton("上一页");
		btn_up.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				attendance.setPageNum(pageSum);
				upPage(getAttendance(attendance).getPrePage());
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, btn_start, 0, SpringLayout.NORTH, btn_up);
		sl_contentPane.putConstraint(SpringLayout.EAST, btn_start, -14, SpringLayout.WEST, btn_up);
		sl_contentPane.putConstraint(SpringLayout.NORTH, btn_up, 24, SpringLayout.SOUTH, jp);
		contentPane.add(btn_up);
		
		btn_next = new JButton("下一页");
		btn_next.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				attendance.setPageNum(pageSum);
				nextPage(getAttendance(attendance).getNextPage());
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, btn_next, 24, SpringLayout.SOUTH, jp);
		sl_contentPane.putConstraint(SpringLayout.EAST, btn_up, -116, SpringLayout.WEST, btn_next);
		contentPane.add(btn_next);
		
		btn_end = new JButton("尾页");
		btn_end.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pageSum = getAttendance(attendance).getPages();
				attendance.setPageNum(pageSum);
				List<ZhAttendance> list = getAttendance(attendance).getList();
				SetTheValue(list);
				
				lb_page.setText("当前是第"+getAttendance(attendance).getPages()+"页");
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, btn_end, 24, SpringLayout.SOUTH, jp);
		sl_contentPane.putConstraint(SpringLayout.EAST, btn_next, -23, SpringLayout.WEST, btn_end);
		sl_contentPane.putConstraint(SpringLayout.EAST, btn_end, -41, SpringLayout.EAST, contentPane);
		contentPane.add(btn_end);
		
		lb_page = new JLabel();
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_page, 2, SpringLayout.NORTH, btn_start);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, lb_page, -3, SpringLayout.SOUTH, btn_start);
		sl_contentPane.putConstraint(SpringLayout.EAST, lb_page, -23, SpringLayout.WEST, btn_next);
		contentPane.add(lb_page);
		
		JLabel lb_se_name = new JLabel("姓名");
		contentPane.add(lb_se_name);
		
		txt_se_name = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, txt_se_name, 50, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_se_name, 118, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_se_name, 3, SpringLayout.NORTH, txt_se_name);
		sl_contentPane.putConstraint(SpringLayout.EAST, lb_se_name, -27, SpringLayout.WEST, txt_se_name);
		contentPane.add(txt_se_name);
		txt_se_name.setColumns(10);
//		txt_se_day.setColumns(10);
		
		JLabel lb_se_starttime = new JLabel("起始时间");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_se_starttime, 53, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lb_se_starttime, 298, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_se_name, -63, SpringLayout.WEST, lb_se_starttime);
		contentPane.add(lb_se_starttime);
		
		txt_se_starttime = new DateChooserJButton();
		sl_contentPane.putConstraint(SpringLayout.NORTH, txt_se_starttime, 0, SpringLayout.NORTH, lb_se_name);
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_se_starttime, 26, SpringLayout.EAST, lb_se_starttime);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_se_starttime, -539, SpringLayout.EAST, contentPane);
		contentPane.add(txt_se_starttime);
//		txt_se_starttime.setColumns(10);
		
		JLabel lb_se_endtime = new JLabel("结束时间");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_se_endtime, 0, SpringLayout.NORTH, lb_se_name);
		sl_contentPane.putConstraint(SpringLayout.WEST, lb_se_endtime, 66, SpringLayout.EAST, txt_se_starttime);
		contentPane.add(lb_se_endtime);
		
		txt_se_endtime = new DateChooserJButton();
		sl_contentPane.putConstraint(SpringLayout.NORTH, txt_se_endtime, 0, SpringLayout.NORTH, lb_se_name);
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_se_endtime, 31, SpringLayout.EAST, lb_se_endtime);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_se_endtime, -252, SpringLayout.EAST, contentPane);
		contentPane.add(txt_se_endtime);
//		txt_se_endtime.setColumns(10);
		
		JButton btn_select = new JButton("查询");
		sl_contentPane.putConstraint(SpringLayout.NORTH, btn_select, -4, SpringLayout.NORTH, lb_se_name);
		sl_contentPane.putConstraint(SpringLayout.WEST, btn_select, 92, SpringLayout.EAST, txt_se_endtime);
		btn_select.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pageSum = 1;
				attendance.setPageNum(pageSum);
				attendance.setU_poid(2);
				if(txt_se_name.getText() != null){
					attendance.setU_name(txt_se_name.getText());
				}
				
				if(txt_se_starttime.getText() != null){
					attendance.setKdate(txt_se_starttime.getText());
				}
				if(txt_se_endtime.getText() != null){
					attendance.setEdate(txt_se_endtime.getText());
				}
				
				List<ZhAttendance> list = attendanceservice.attendanceSelectByTiaojian(attendance).getList();
				SetTheValue(list);
				
				
			}
		});
		contentPane.add(btn_select);
		attendance.setPageNum(pageSum);
		List<ZhAttendance> list = getAttendance(attendance).getList();	
		SetTheValue(list);
	
	}
	
	public PageInfo<ZhAttendance> getAttendance(ZhAttendance att){
		att.setU_poid(2);
		PageInfo<ZhAttendance> pageinfo = attendanceservice.attendanceSelectAll(att);
		return pageinfo;
	}
	
	public void nextPage(int poid){
		  
		  pageSum++; 
		  attendance.setPageNum(pageSum);
		  List<ZhAttendance> list = getAttendance(attendance).getList();
		  if(list.size() == 0){
		   JOptionPane.showMessageDialog(this, "当前已是最后一页！");
		   pageSum--;
		   attendance.setPageNum(pageSum);
		   list = getAttendance(attendance).getList();
		   SetTheValue(list);
		   
		   return;
		  }
		  SetTheValue(list);
		 }
		 

		 
		 public void upPage(int poid){
		  if(pageSum<=1){
		   JOptionPane.showMessageDialog(this, "当前是第一页！");
		   attendance.setPageNum(pageSum);
		   List<ZhAttendance> student = getAttendance(attendance).getList();
		   SetTheValue(student);
		   return;
		  }
		  pageSum--;
		  attendance.setPageNum(pageSum);
		  List<ZhAttendance> student = getAttendance(attendance).getList();
		  SetTheValue(student);
		  
		 }
		 public void SetTheValue(List<ZhAttendance> list){
			
				lb_page.setText("当前是第"+pageSum+"页");
				
			datas = new Object[list.size()][7];
				if(list.size()>0){
					for (int i = 0; i < list.size(); i++)
					{
						for (int j = 0; j < 7; j++)
						{
							switch (j)
							{
							case 0:
								datas[i][j] = list.get(i).getAt_id();
								break;
							case 1:
								datas[i][j] = list.get(i).getU_name();
								break;
							case 2:
								datas[i][j] = list.get(i).getAt_amtype1();
								break;
							case 3:
								datas[i][j] = list.get(i).getAt_noontype1();
								break;
							case 4:
								datas[i][j] = list.get(i).getAt_pmtype1();
								break;
							case 5:
								datas[i][j] = list.get(i).getAt_nighttype1();
								break;
							case 6:
								
								datas[i][j] = list.get(i).getAt_days();
								break;
							
							
							}
						}
					}
				}
			DefaultTableModel dftablemodel = new DefaultTableModel(datas,titles);
			jt.setModel(dftablemodel);
			}

}
